package com.metaera.app.ui.activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.CompoundButton;

import com.metaera.app.R;
import com.metaera.app.controller.presenter.EditUserNoticePresenter;
import com.metaera.app.controller.presenter.UserNoticePresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.UserNoticeResponse;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.NotificationsUtils;
import com.metaera.app.utils.ViewUtils;

import androidx.appcompat.widget.SwitchCompat;
import androidx.core.app.NotificationManagerCompat;


public class PushManagerActivity extends BaseActivity {

    private SwitchCompat switchMsgAll;

    private EditUserNoticePresenter mNoticePresenter;


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_push_manager;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ActivityTitleHelper.setTitle(this, R.string.push_manager);

        // find
        switchMsgAll = findViewById(R.id.switch_msg_all);
        SwitchCompat switch_news_notify = findViewById(R.id.switch_news_notify);
        SwitchCompat switch_flash_notify = findViewById(R.id.switch_flash_notify);
        SwitchCompat switch_msg_notify = findViewById(R.id.switch_msg_notify);

        // listener
        findViewById(R.id.layout_author_notify)
                .setOnClickListener(v -> startActivity(PushFollowAuthorActivity.class));

        // 跳转到通知设置页面
        switchMsgAll.setOnClickListener(v -> NotificationsUtils.openPush(this));

        UserNoticePresenter presenter = new UserNoticePresenter();
        presenter.setModelAndView(data -> {
            switch_news_notify.setChecked(data.getIs_article() == 1); // 文章
            switch_flash_notify.setChecked(data.getIs_news_flash() == 1); // 快讯
            switch_msg_notify.setChecked(data.getIs_notice() == 1); // 消息中心

            mNoticePresenter = new EditUserNoticePresenter(data.getId());
            mNoticePresenter.setModelAndView(new BasicNetworkRequestView<>());

            switch_news_notify.setOnCheckedChangeListener((v, isChecked) -> mNoticePresenter.article(isChecked));
            switch_flash_notify.setOnCheckedChangeListener((v, isChecked) -> mNoticePresenter.flash(isChecked));
            switch_msg_notify.setOnCheckedChangeListener((v, isChecked) -> mNoticePresenter.center(isChecked));
        });

        presenter.execute();

        // init
        // renderLayout();
    }

    @Override
    protected void onStart() {
        super.onStart();
        renderLayout();
    }

    private void renderLayout() {
        View layout_news_notify = findViewById(R.id.layout_news_notify);
        View layout_flash_notify = findViewById(R.id.layout_flash_notify);
        View layout_msg_notify = findViewById(R.id.layout_msg_notify);
        View layout_author_notify = findViewById(R.id.layout_author_notify);
        View line_2 = findViewById(R.id.line_2);
        View line_3 = findViewById(R.id.line_3);
        View line_4 = findViewById(R.id.line_4);
        View line_5 = findViewById(R.id.line_5);

        // init
        boolean notificationEnabled = NotificationsUtils.isNotificationEnabled(this);
        switchMsgAll.setChecked(notificationEnabled);
        ViewUtils.setVisibility(notificationEnabled ? View.VISIBLE : View.GONE,
                layout_author_notify, layout_news_notify, layout_flash_notify, layout_msg_notify,
                line_2, line_3, line_4, line_5);

        if (mNoticePresenter != null){
            mNoticePresenter.notice(notificationEnabled);
        }
    }

}
